package com.curso.clima;

import com.curso.clima.service.WeatherService;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;


public class WeatherWidget extends AppWidgetProvider  {

	public static String ACTION_WIDGET_UPDATE = "ACTION_WIDGET_UPDATE";
	public static String ACTION_WIDGET_BTN_UPDATE = "BTN_ACTION_WIDGET_UPDATE";


	@Override
	public void onUpdate(Context context,
			AppWidgetManager appWidgetManager,
			int[] appWidgetIds)
	{
		try{
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.weather_widget2);

			// set button update
			Intent intentUpdate = new Intent(context, WeatherWidget.class);
			intentUpdate.setAction(ACTION_WIDGET_BTN_UPDATE);
			PendingIntent mPendingIntent1 = PendingIntent.getBroadcast(context, 0, intentUpdate, 0);
			remoteViews.setOnClickPendingIntent(R.id.button_update, mPendingIntent1);

			// set button config
			Intent configIntent = new Intent(context, UserSettingsActivity.class);
			PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
			remoteViews.setOnClickPendingIntent(R.id.button_config, configPendingIntent);
			
			// Update the widget
			appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

			// execute service
			iniciarServicio(context);
		}
		catch(Exception ex){
			Log.i("Weather", "Widget update");
		}
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// v1.5 fix that doesn't call onDelete Action
		final String action = intent.getAction();

		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) 
		{
			final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) 
			{
				this.onDeleted(context, new int[] { appWidgetId });
			}			
		}

		if(action.equals(ACTION_WIDGET_UPDATE)){
			Log.i("Clima","Recibe action UPDATE");

			String temperatura = intent.getStringExtra("temperatura");
			String ciudad = intent.getStringExtra("ciudad");

			Log.d("Clima", "Temperatura recibida: " + temperatura );
			Log.d("Clima", "Ciudad:" + ciudad);

			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.weather_widget2);
			remoteViews.setTextViewText(R.id.widget_temperatura, temperatura);
			remoteViews.setTextViewText(R.id.ciudad, ciudad);

			ComponentName thisWidget = new ComponentName(context, WeatherWidget.class);
			AppWidgetManager manager = AppWidgetManager.getInstance(context);
			manager.updateAppWidget(thisWidget, remoteViews);

		}else if(action.equals(ACTION_WIDGET_BTN_UPDATE)){
			iniciarServicio(context);
		}

		super.onReceive(context, intent);
	}

	private void iniciarServicio(Context context){
		Intent intentService = new Intent(context, WeatherService.class);
		context.startService(intentService);	
	}
}
